//
//  GJSegmentControl.m
//  YDGJ
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "GJSegmentControl.h"

#define sg_start_Y (64)
#define sg_H (40)
#define sg_line_H (2)
#define sg_base_Tag (20000)

@interface GJSegmentControl ()

@property (nonatomic, strong) UIProgressView *prgView;

@property (nonatomic, copy) void (^blk)(NSInteger);

@end

@implementation GJSegmentControl

+ (instancetype)segmentControlWithTitles:(NSArray *)titles selectedBlock:(void(^)(NSInteger))selectedBlk
{
    return [[self alloc] initWithTitles:titles selectedBlock:selectedBlk];
}

- (instancetype)initWithTitles:(NSArray *)titles selectedBlock:(void(^)(NSInteger))selectedBlk
{
    if (!titles||![titles isKindOfClass:[NSArray class]]||([titles isKindOfClass:[NSArray class]]&&titles.count==0)) {
        return nil;
    }
    if (self = [super initWithFrame:CGRectMake(0, sg_start_Y, Screen_Width, sg_H)]) {
        self.blk = selectedBlk;
        NSInteger itemCount = titles.count;
        CGFloat itemWidth = (Screen_Width)/(CGFloat)itemCount;
        for (int i = 0; i < titles.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = sg_base_Tag+i;
            [btn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
            [btn setTitleColor:Color_Main forState:UIControlStateSelected];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            [btn.titleLabel setFont:FontCustomSize(14)];
            [btn setFrame:CGRectMake(i*itemWidth, 0, itemWidth, (sg_H-sg_line_H))];
            WEAKSELF
            [btn addActionHandler:^(NSInteger tag) {
                for (UIView *view in weakSelf.subviews) {
                    if ([view isKindOfClass:[UIButton class]]) {
                        ((UIButton *)view).selected = view.tag==tag;
                        view.userInteractionEnabled = !(view.tag==tag);
                    }
                }
                weakSelf.userInteractionEnabled = NO;
                [UIView animateWithDuration:0.2 animations:^{
                    weakSelf.prgView.frame = CGRectMake(itemWidth*(tag-sg_base_Tag), (sg_H-sg_line_H), itemWidth, 2);
                } completion:^(BOOL finished) {
                    if (finished) {
                        weakSelf.userInteractionEnabled = YES;
                    }
                }];
                if (weakSelf.blk) {
                    weakSelf.blk(tag-sg_base_Tag);
                }
            }];
            btn.selected = NO;
            btn.userInteractionEnabled = YES;
            [self addSubview:btn];
            if (i==0) {
                btn.selected = YES;
                btn.userInteractionEnabled = NO;
            }
        }
        
        _prgView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, (sg_H-sg_line_H), itemWidth, 2)];
        _prgView.progressTintColor = Color_Main;
        _prgView.progress = 1;
        [self addSubview:_prgView];
        
        for (int i = 0; i < itemCount; i++) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(itemWidth*i-0.25, 8, 0.5, self.height-16)];
            line.backgroundColor = RGB(229,229, 229);
            [self addSubview:line];
        }
        
        UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, sg_H, Screen_Width, 0.5)];
        line2.backgroundColor = RGB(229,229, 229);
        [self addSubview:line2];
    }
    return self;
}

- (void)clickButtonAtIndex:(NSInteger)index
{
    UIButton *btn = (UIButton *)[self viewWithTag:sg_base_Tag+index];
    if ([btn respondsToSelector:@selector(actionTouched:)]) {
        [btn performSelector:@selector(actionTouched:) withObject:btn];
    }
}

@end
